﻿// ---------------------------------------------------------------------
// Lớp STurnManager
//
// Nội dung:
//    + Lớp quản lý lượt
//
// Copyright (c) 2009 YugiOH
//
// Nguyễn Hữu Lê Trọng Tín      YM: ilu2009             ĐT: 0906520611 
// Võ Phước Hậu                 YM: phuochauspkt        ĐT: 0979202707
// ---------------------------------------------------------------------


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;
using Common;

namespace ServerYuGi
{
    public partial class STurnManager
    {
        private static Dictionary<EnumState, ITurn> Factory = new Dictionary<EnumState, ITurn>();
        private static EnumState[] Data = { EnumState.DrawPhase,EnumState.StandbyPhase,EnumState.MainPhase1,EnumState.BattlePhase,EnumState.MainPhase2,EnumState.EndTurn };
        private SSupervisor super;
        private int index = 0;
        private int turnID = 0;
        public int IDPlayerTrongLuot { get; set; }
        
        public STurnManager(SSupervisor super)
        {
            this.super = super;
            DrawPhase draw = new DrawPhase();
            BattlePhase battle = new BattlePhase();
            MainPhase1 mainphase1 = new MainPhase1();
            StandbyPhase standbyphase = new StandbyPhase();
            MainPhase2 mainphase2 = new MainPhase2();
            ServerYuGi.EndTurn endturn = new EndTurn();
        }        
       
        public static void Register(EnumState e, ITurn exe)
        {
            if (Factory.ContainsKey(e))
                Factory[e] = exe;
            else
            {
                Factory.Add(e, exe);
                
            }
        }        
        public void NextTurn()
        {
            EndTurn();
            API_Turn(false); // xu ly endturn
            index++;
            if (index == Data.Count() - 1)
            {
                ITurn turn = Factory[Data[index]];
                
                turn.SBeginTurn(super);
                API_Turn(true);
                index = 0;
                QuaLuotMoi();
            }
            else
            {
                ITurn turn = Factory[Data[index]];
                turn.SBeginTurn(super);
                API_Turn(true);
            }
        }

        /// <summary>
        /// Nhảy tới 1 turn mới
        /// </summary>
        /// <param name="e">Turn mới</param>
        public void GoToTurn(EnumState e)
        {
            EndTurn();
            for (int i = 0; i < Data.Count(); i++)
            {
                if (Data[i] == e)
                {
                    index = i;
                    break;
                }
            }
            ITurn turn = Factory[Data[index]];
            turn.SBeginTurn(super);
           
            if (index == Data.Count() - 1)
            {
                index = 0;
                QuaLuotMoi();
            }
            
        }
        public void QuaLuotMoi()
        {

            Console.WriteLine("-----Qua Luot Moi-----");
            SPlayer player = super.LayPlayerKhacVoiPlayerDangTrongLuot();
            IDPlayerTrongLuot = player.ID;
            ITurn turn = Factory[Data[index]];
            turn.SBeginTurn(super);
            turnID++;
            super.logManager.LogTurnID(turnID);
            API_Turn(true);
            super.DrawPhase();      
            //Reset & Init Block
            player.Block.Reset();
            player.InitDuocPhepDoiTrangThai();
            player.InitDuocPhepTanCong();
        }

        private void EndTurn()
        {
            ITurn turn = Factory[Data[index]];
            turn.SEndTurn(super);
        }

        /// <summary>
        /// Refresh Turn
        /// </summary>
        public void F5()
        {
            index = 0;
            turnID = 0;
        }

        /// <summary>
        /// Lấy ITurn hiện tại
        /// </summary>
        /// <returns>ITurn hiện tại</returns>
        public ITurn GetITurnNow()
        {
            ITurn turn = Factory[Data[index]];
            return turn;
        }
        public EnumState GetStateNow()
        {
            return Data[index];
        }
        private void API_Turn(bool type,params object[] param)  // type = true,begin,type = false,end
        {
            string message = "";
            if (type)
                message = "BEGIN";
            else
                message = "END";
            Type t = typeof(STurnManager);
            MethodInfo handler = t.GetMethod("Process_" + Data[index].ToString() +"_" + message.ToString(), BindingFlags.NonPublic | BindingFlags.Instance);
            handler.Invoke(this, new object[] { param });
        }

    }
}
